<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Models\Artists\ArtistsPhoto;
use App\Models\Artists\ArtistsUserClass;
use App\Models\Artists\JoinArtist;
use Encore\Admin\Form;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use App\Models\Category;
use App\Admin\Extensions\Tools\GridView;

class PhotoController extends Controller
{
    protected $name = '相册管理';
    use ModelForm;
	
	public $user_id = 0;
	
	public function __construct()
	{
		//查询艺术家的user_id
		$info = JoinArtist::where('artists_id', request()->get('artists_id'))->first();
		if($info)
		{
			$this->name = $info->name.'的'.$this->name;
			$this->user_id = $info->user_id;
		}
		
	}
    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description($this->name);


            $content->body($this->grid());
        });
    }
    
    /**
     * Edit interface.
     *
     * @param $id
     *
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->name);
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->name);
            $content->description('创建');

            $content->body($this->form());
        });
    }
    
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ArtistsPhoto::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            
            $grid->filter(function($filter){
                
                $filter->like('name', '标题');
                //$filter->addFilter($filter->like('post_title', '标题'));
                $filter->equal('artists_id', '艺术家ID');
                
            
            });
			$grid->tools(function ($tools) {
				// dd($tools);
				$tools->append('<a href="/admin/artistsuseclass?class_type=1&artists_id='.request()->get('artists_id').'" class="btn btn-sm btn-success">
					<i class="fa fa-save"></i>&nbsp;&nbsp;分类管理
				</a>');
				$tools->append('<a href="/admin/joinartist?artists_id='.request()->get('artists_id').'" class="btn btn-sm btn-success">
					<i class="fa fa-save"></i>&nbsp;&nbsp;返回艺术家
				</a>');
			});
            $grid->title('标题');
            //$grid->title()->limit(20);

            $grid->image('图片')->display(function(){
                return $this->thumb;
            })->image();
			$grid->sys_class('分类')->display(function(){
                if($this->artists_class_id > 0 && !empty($this->classname))
				{
					return $this->classname['name'];
				}
				return '无分类';
            });

            $grid->created_at('创建时间');

            $grid->tools(function ($tools) {
                $tools->append(new GridView());
            });

            if (request()->get('view') !== 'table') {
                $grid->setView('admin.grid.card');
            }
            // $grid->column('id', 'ID')->sortable();
            // $grid->column('listorder', '排序')->editable();
            
            // $grid->column('title', '名称');
            // $grid->column('thumb', '图片地址');
            // $grid->column('status','状态')->options()->select(ArtistsPhoto::$status);
            
            $grid->column('updated_at', '更新时间');
        });
    }
    
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        
        return Admin::form(ArtistsPhoto::class, function (Form $form) {
            
            $form->number('id', 'ID')->attribute('disabled', false);
            $form->text('title', '名称')->rules('required|min:1');
            $form->image('thumb','图片链接')->uniqueName();
            $form->number('listorder', '排序')->help('数字越大排前');
			//获取当前艺术家的user_id
			$info = JoinArtist::where('artists_id', request()->get('artists_id'))->first();
			
			if($info && $info->user_id>0)
			{
				$ArtistsUserClass = new ArtistsUserClass();
				$form->select('artists_class_id', '分类')->options($ArtistsUserClass->selectOptionss($info->user_id,1))->rules('required|min:0');
			}
			
			// dd($ArtistsUserClass->selectOptionss(71119));
			// $form->select('artists_class_id', '分类')->options(ArtistsUserClass::withQuery(function($query){
				// dd($query);
			// })->selectOptions())->rules('required|min:0');
			
			$form->hidden('artists_id')->default(request()->get('artists_id', 0));
			$form->saving(function(Form $form){
				// $form->ignore(['content']);
				
				
				
				// $form->ignore(['content']);
				// dd($form->content);
				// if($form->content['thumb'] == '')
				// {
					// $form->content['thumb'] = $form->model()->content['thumb'];
				// }
				// dd($form->content);
			});
			
            
        });
    }
}
